package com.hcryan.djm.exception;

import com.alibaba.fastjson.JSON;

import java.util.HashMap;
import java.util.Map;


/**
 * @author ryanwdj
 * @since 2025/2/18
 */
public class BusinessException extends RuntimeException {
    private static final long serialVersionUID = 1L;
    private int code;
    private String message;

    public BusinessException(int code, String message) {
        this(code, message, false);
    }

    public BusinessException(int code, String message, boolean toJsonMessage) {
        this.code = code;
        this.message = message;
        if (toJsonMessage) {
            this.toJsonMessage();
        }

    }

    public void toJsonMessage() {
        Map<String,Object> objectNode=new HashMap<>();
        objectNode.put("exceptionJsonMessage", true);
        objectNode.put("code", this.code);
        objectNode.put("message", this.message);
        this.message = JSON.toJSONString(objectNode);
    }

    private BusinessException() {
    }

    public int getCode() {
        return this.code;
    }

    public String getMessage() {
        return this.message;
    }

    public void setCode(final int code) {
        this.code = code;
    }

    public void setMessage(final String message) {
        this.message = message;
    }

    public String toString() {
        return "BusinessException(code=" + this.getCode() + ", message=" + this.getMessage() + ")";
    }

    public boolean equals(final Object o) {
        if (o == this) {
            return true;
        } else if (!(o instanceof BusinessException)) {
            return false;
        } else {
            BusinessException other = (BusinessException)o;
            if (!other.canEqual(this)) {
                return false;
            } else if (this.getCode() != other.getCode()) {
                return false;
            } else {
                Object thisMessage = this.getMessage();
                Object otherMessage = other.getMessage();
                if (thisMessage == null) {
                    if (otherMessage != null) {
                        return false;
                    }
                } else if (!thisMessage.equals(otherMessage)) {
                    return false;
                }

                return true;
            }
        }
    }

    protected boolean canEqual(final Object other) {
        return other instanceof BusinessException;
    }

    public int hashCode() {
        int result = 1;
        result = result * 59 + this.getCode();
        Object $message = this.getMessage();
        result = result * 59 + ($message == null ? 43 : $message.hashCode());
        return result;
    }

}
